/*! \file Library.cpp
 *	\brief
 *
 *	created:	2009/11/12
 *	created:	12:11:2009   15:55
 *	filename: 	c:\src\main\libs\utils\Library.cpp
 *	file path:	c:\src\main\libs\utils
 *	file base:	Library
 *	file ext:	cpp
 *	author:		Vitalii Trubchaninov
 *				venngraydog@gmail.com
 */

#include "utils/stdafx.h"
#include "utils/Library.h"

#if defined(TT_UNIX)
#   include <dlfcn.h>
#endif //defined(TT_UNIX)

namespace tt{
namespace utils{

Library::Library(const String& libraryPath)
: m_library( NULL)
{
#if defined(TT_WIN32)
	m_library = LoadLibrary(libraryPath.c_str());
#elif defined(TT_UNIX)
 	m_library = dlopen(libraryPath.c_str(), RTLD_LOCAL | RTLD_LAZY);
#endif //defined(TT_WIN32)
}

Library::~Library(void)
{
	if (m_library)
	{
#if defined(TT_WIN32)
		FreeLibrary(m_library);
#elif defined(TT_UNIX)
		dlclose(m_library);
#endif //defined(TT_WIN32)
	}
}

bool Library::valid() const
{
	return m_library != NULL;
}

Library::operator LibraryHandler() const
{
	return m_library;
}

void* Library::GetProcAddress(const Char* functionName)
{
#if defined(TT_WIN32)
	return ::GetProcAddress(m_library, functionName);
#elif defined(TT_UNIX)
	return dlsym(m_library, functionName);
#endif //defined(TT_WIN32)
}

void* Library::GetProcAddress(const String& functionName)
{
	return GetProcAddress(functionName.c_str());
}


} //namespace utils
} //namespace tt
